package me.jessyan.mvparms.demo.mvp.ui.activity;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

import com.jess.arms.di.module.ClientModule;
import com.platform.ai.control.R;

import java.io.IOException;

import me.jessyan.mvparms.demo.mvp.model.logic.TextUtil;
import me.jessyan.retrofiturlmanager.RetrofitUrlManager;
import retrofit2.Retrofit;

public class MediaService extends Service {
    private MediaPlayer mPlayer;
    private Context mContext;
    /* 绑定服务的实现流程：
     * 1.服务 onCreate， onBind， onDestroy 方法
     * 2.onBind 方法需要返回一个 IBinder 对象
     * 3.如果 Activity 绑定，Activity 就可以取到 IBinder 对象，可以直接调用对象的方法
     */
    // 相同应用内部不同组件绑定，可以使用内部类以及Binder对象来返回。
    public class MusicController extends Binder {
        public void play() {
            if(!TextUtil.isEmpty(MainActivity.MUSIC_URL)) {
                mPlayer = MediaPlayer.create(mContext, Uri.parse(MainActivity.MUSIC_URL));
            }else{
                mPlayer = MediaPlayer.create(mContext, R.raw.spz);
            }
            mPlayer.start();//开启音乐
        }
        public void pause() {
            mPlayer.pause();//暂停音乐
        }
        public long getMusicDuration() {
            return mPlayer.getDuration();//获取文件的总长度
        }
        public long getPosition() {
            return mPlayer.getCurrentPosition();//获取当前播放进度
        }
        public void setPosition (int position) {
            mPlayer.pause();
            mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mPlayer.setOnSeekCompleteListener(new MediaPlayer.OnSeekCompleteListener() {
                public void onSeekComplete(MediaPlayer m) {
                    mPlayer.start();
                }
            });
            mPlayer.seekTo(position);

        }
        public void destroy () {
            mPlayer.pause();
        }
        public void release () {
            mPlayer.release();
        }
        public boolean isPlay () {
            return mPlayer.isPlaying();
        }
        public void pre () {
            if(mPlayer != null) {
                mPlayer.pause();
                mPlayer.release();
                if (MainActivity.MUSIC_LABEL.equals("生僻字")) {
                    MainActivity.MUSIC_LABEL = "口是心非";
                    mPlayer = MediaPlayer.create(mContext, R.raw.ksxf);
                } else if (MainActivity.MUSIC_LABEL.equals("稻香")) {
                    MainActivity.MUSIC_LABEL = "生僻字";
                    mPlayer = MediaPlayer.create(mContext,R.raw.spz);
                } else if (MainActivity.MUSIC_LABEL.equals("口是心非")) {
                    MainActivity.MUSIC_LABEL = "稻香";
                    mPlayer = MediaPlayer.create(mContext, R.raw.dx);
                }else{
                    mPlayer = MediaPlayer.create(mContext, R.raw.ksxf);
                }
                play();
            }else{
                mPlayer = MediaPlayer.create(mContext, R.raw.ksxf);
                play();
            }
        }
        public void next(){
            if(mPlayer != null) {
                mPlayer.pause();
                mPlayer.release();
                if (MainActivity.MUSIC_LABEL.equals("生僻字")) {
                    MainActivity.MUSIC_LABEL = "稻香";
                    mPlayer = MediaPlayer.create(mContext, R.raw.dx);
                } else if (MainActivity.MUSIC_LABEL.equals("稻香")) {
                    MainActivity.MUSIC_LABEL = "口是心非";
                    mPlayer = MediaPlayer.create(mContext, R.raw.ksxf);
                } else if (MainActivity.MUSIC_LABEL.equals("口是心非")) {
                    MainActivity.MUSIC_LABEL = "生僻字";
                    mPlayer = MediaPlayer.create(mContext, R.raw.spz);
                }
            }else{
                mPlayer = MediaPlayer.create(mContext,R.raw.dx);
            }
            play();
        }

    }
    /**
     * 当绑定服务的时候，自动回调这个方法
     * 返回的对象可以直接操作Service内部的内容
     * @param intent
     * @return
     */
    @Override
    public IBinder onBind(Intent intent) {
        return new MusicController();
    }
    @Override
    public void onCreate() {
        super.onCreate();
        this.mContext = this;

        if (MainActivity.MUSIC_LABEL.contains("生僻字")){
            mPlayer = MediaPlayer.create(this, R.raw.spz);
        }else if (MainActivity.MUSIC_LABEL.contains("稻香")){
            mPlayer = MediaPlayer.create(this, R.raw.dx);
        }else if (MainActivity.MUSIC_LABEL.contains("口是心非")){
            mPlayer = MediaPlayer.create(this, R.raw.ksxf);
        }else {
            mPlayer = MediaPlayer.create(this, R.raw.spz);
        }
        mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    }


    /**
     * 任意一次unbindService()方法，都会触发这个方法
     * 用于释放一些绑定时使用的资源
     * @param intent
     * @return
     */
    @Override
    public boolean onUnbind(Intent intent) {
        return super.onUnbind(intent);
    }
    @Override
    public void onDestroy() {
        if (mPlayer.isPlaying()) {
            mPlayer.stop();
        }
        mPlayer.release();
        mPlayer = null;
        super.onDestroy();
    }

}